<?php 
	class Manage_gallery extends  CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function index(){
			redirect("manage_gallery/gallery_list");
		}
		
		public function gallery_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item,$num,$page,$total) = $this->db_news->gallery_item_list();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("item",$item);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
				
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_gallery/gallery_item_delAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/news/gallery_list.html');
		}
		
		public function gallery_add($cid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("title_en","name","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
			
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("customer",set_value("customer"));
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("url",set_value('url'));
			
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					//$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errorstatus",form_error('status'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/gallery/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 250;
							$config['height'] = 100;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post("customer"),
								"title_en"=>trim($this->input->post('title_en')),
								"title_zh_CN"=>trim($this->input->post('title_cn')),
								"title_th"=>trim($this->input->post('title_th')),
								"img"=>$picnamethumb,
								"img_big"=>$picname,
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('mobile_gallery',$data);
						if($res){
							if($cid != ""){
								redirect("manage_gallery/gallery_view/$cid");
							}else{
							redirect('manage_gallery/gallery_list/success');
							}
						}
						else  redirect('manage_gallery/gallery_list/fail');
					}
				}
			}//end post
			
			$this->cismarty->assign("customer",$this->db_news->customer_list($cid));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_gallery/gallery_add/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/news/gallery_add.html');
		}
		
		public function gallery_edit($id,$cid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("title_en","name","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("customer",set_value("customer"));
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					//$this->cismarty->assign("url",set_value('url'));
						
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					//$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errorstatus",form_error('status'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/gallery/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 250;
							$config['height'] = 100;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("img"=>$picnamethumb,"img_big"=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('mobile_gallery',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post("customer"),
								"title_en"=>trim($this->input->post('title_en')),
								"title_zh_CN"=>trim($this->input->post('title_cn')),
								"title_th"=>trim($this->input->post('title_th')),
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_gallery',$data);
						if($res){
							if($cid != ""){
								redirect("manage_gallery/gallery_view/$cid");
							}else{
								redirect('manage_gallery/gallery_list/success');
							}
						}
						else  redirect('manage_gallery/gallery_list/fail');
					}
				}
			}//end post
			else{
				$this->db->where("pkid",$id);
				$row = $this->db->get("mobile_gallery")->row();
				$this->cismarty->assign("title_en",$row->title_en);
				$this->cismarty->assign("title_cn",$row->title_zh_CN);
				$this->cismarty->assign("title_th",$row->title_th);
				if(trim($row->img) != ""){
 				$this->cismarty->assign("img",'<img src="'.base_url().'media/gallery/'.$row->img.'"><br>');
	 			}
	 			if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
	 			else $this->cismarty->assign("sd",'checked="checked"');
				$cid = $row->customer_pkid;
			}
				
			$this->cismarty->assign("customer",$this->db_news->customer_list($cid));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_gallery/gallery_edit/$id/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/news/gallery_edit.html');
		}
		
		public function gallery_view($cid){
			$arr = array();
			$this->db->where("customer_pkid",$cid);
			$this->db->order_by("pkid","desc");
			$res =$this->db->get("mobile_gallery")->result_array();
			foreach ($res as $P){
				$tmp['pkid']		=$P['pkid'];
				$tmp['img']		='<img src="'.base_url().'media/gallery/'.$P['img'].'">';
				$tmp['title']		=$P['title_en'];
				$arr[] =$tmp;
			}
		
			$this->cismarty->assign("gallery",$arr);
			$this->cismarty->assign("total",$this->db_news->gallery_count($cid));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("cid",$cid);
			$this->cismarty->assign("form_open",form_open_multipart("manage_gallery/gallery_dell_all/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/news/gallery_view.html');
		}
		public function gallery_del($id,$cid){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_gallery");
				if($res) redirect("manage_gallery/gallery_view/$cid");
			}
		}
		
		public function gallery_dell_all($cid){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_gallery");
				}
			}
			redirect("manage_gallery/gallery_view/$cid");
		}
	}
?>